package com.cskaoyan.service.admin;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketPermissionMapper;
import com.cskaoyan.mapper.MarketRoleMapper;
import com.cskaoyan.mapper.PermissionShowMapper;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.admin.bo.PermissionsBo;
import com.cskaoyan.model.admin.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/11/29 19:55
 */
@Service
public class SystemRoleServiceImpl implements SystemRoleService {
    @Autowired
    MarketRoleMapper marketRoleMapper;
    @Autowired
    MarketAdminMapper marketAdminMapper;
    @Autowired
    PermissionShowMapper permissionShowMapper;
    @Autowired
    MarketPermissionMapper marketPermissionMapper;

    @Override
    public BaseDataListVo getRoles() {
        MarketRoleExample example = new MarketRoleExample();
        BaseDataListVo baseData = new BaseDataListVo();
//        example.createCriteria().andIdLessThan(Integer.MAX_VALUE);
        List<RoleOptionVo> roleOptionVos = new ArrayList<>();
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        for (MarketRole marketRole : marketRoles) {
            RoleOptionVo roleOptionVo = new RoleOptionVo();
            roleOptionVo.setLabel(marketRole.getName());
            roleOptionVo.setValue(marketRole.getId());
            roleOptionVos.add(roleOptionVo);
        }
        PageInfo pageInfo = new PageInfo(marketRoles);
        baseData.setList(roleOptionVos);
        baseData.setTotal((int) pageInfo.getTotal());
        baseData.setLimit(pageInfo.getSize());
        baseData.setPages(pageInfo.getPages());
        baseData.setPage(pageInfo.getPageNum());
        return baseData;
    }

    @Override
    public BaseDataListVo roleList(BaseParam baseParam, String name) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        MarketRoleExample example = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = example.createCriteria().andDeletedEqualTo(false);
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(marketRoles);
        BaseDataListVo baseDataListVo = new BaseDataListVo();
        baseDataListVo.setList(marketRoles);
        baseDataListVo.setLimit(baseParam.getLimit());
        baseDataListVo.setTotal((int) pageInfo.getTotal());
        baseDataListVo.setPage(baseParam.getPage());
        baseDataListVo.setPages(baseDataListVo.getPages());
        return baseDataListVo;

    }

    @Override
    public MarketRole roleCreate(RoleCreateVo roleCreateVo) {
        MarketRole marketRole = new MarketRole();
        Date date = new Date();
        marketRole.setDesc(roleCreateVo.getDesc());
        marketRole.setName(roleCreateVo.getName());
        marketRole.setId(null);
        marketRole.setAddTime(date);
        marketRole.setUpdateTime(date);
        marketRole.setDeleted(false);
        marketRole.setEnabled(true);
        marketRoleMapper.insertSelective(marketRole);
        return marketRole;
    }

    @Override
    public void roleUpdate(MarketRole marketRole) {
        MarketRoleExample example = new MarketRoleExample();
        example.createCriteria().andNameEqualTo(marketRole.getName());
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        if (marketRoles.size() != 0) {
            throw new IllegalArgumentException();
        }
        marketRole.setUpdateTime(new Date());
        marketRoleMapper.updateByPrimaryKeySelective(marketRole);
    }


    @Override
    public void deleteRoles(MarketRole marketRole) {
        MarketAdminExample example = new MarketAdminExample();
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example);
        for (MarketAdmin marketAdmin : marketAdmins) {
            String roleIds = marketAdmin.getRoleIds();
            String[] split = roleIds.replace("[", "").replace("]", "").split(",");
            for (String s : split) {
                if (marketRole.getId().toString().equals(s)) {
                    throw new IllegalArgumentException();
                }
            }
        }
//        MarketPermissionExample example = new MarketPermissionExample();
//        example.createCriteria().andRoleIdEqualTo(marketRole.getId());
//        List<MarketPermission> marketPermissions = marketPermissionMapper.selectByExample(example);

//        if (marketPermissions.size()!=0){
//            throw new IllegalArgumentException();
//        }
        marketRole.setDeleted(true);
        marketRole.setUpdateTime(new Date());
        marketRoleMapper.updateByPrimaryKeySelective(marketRole);
    }

    @Override
    public PermissionsVo rolePermissions(Integer roleId) {

        List<Level1PermissionsVo> level1PermissionsVos = permissionShowMapper.selectLevel1PermissionVo();
        //一级循环
        for (Level1PermissionsVo level1PermissionsVo : level1PermissionsVos) {
            level1PermissionsVo.setLabel(level1PermissionsVo.getId());
            List<Level2PermissionsVo> level2PermissionsVos = permissionShowMapper.selectLevel2Permission(level1PermissionsVo.getLabel());
            //二级循环
            for (Level2PermissionsVo level2PermissionsVo : level2PermissionsVos) {
                level2PermissionsVo.setLabel(level2PermissionsVo.getId());
                List<Level3PermissionsVo> level3PermissionsVos = permissionShowMapper.selectLevel3Permission(level2PermissionsVo.getLabel());
                level2PermissionsVo.setChildren(level3PermissionsVos);
            }
            level1PermissionsVo.setChildren(level2PermissionsVos);
        }

        MarketPermissionExample example = new MarketPermissionExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        List<MarketPermission> marketPermissions = marketPermissionMapper.selectByExample(example);
        List<String> permissions = new ArrayList<>();
        for (MarketPermission marketPermission : marketPermissions) {
            permissions.add(marketPermission.getPermission());
        }
        PermissionsVo permissionVo = new PermissionsVo();
        permissionVo.setSystemPermissions(level1PermissionsVos);
        permissionVo.setAssignedPermissions(permissions);
        return permissionVo;
    }

    @Override
    public void updatePermissions(PermissionsBo permissionsBo) {
        List<String> permissions = permissionsBo.getPermissions();
        MarketPermissionExample example = new MarketPermissionExample();
        marketPermissionMapper.deleteByPrimaryKey(permissionsBo.getRoleId());

        for (String permission : permissions) {
            MarketPermission marketPermission = new MarketPermission();
            Date date = new Date();
            marketPermission.setPermission(permission);
            marketPermission.setUpdateTime(date);
            marketPermission.setAddTime(date);
            marketPermission.setDeleted(false);
            marketPermission.setRoleId(permissionsBo.getRoleId());
            marketPermissionMapper.insertSelective(marketPermission);
        }

    }
}
